//
//  ForgetPasswordViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/23.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "ResetPasswordViewController.h"
#import "HWZBAPIRequest.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "MBProgressHUD+Toast.h"
#import "VerifyButton.h"

@interface ForgetPasswordViewController ()

@property (strong, nonatomic) UITextField *mobilePhoneTextField;
@property (strong, nonatomic) UITextField *verifyCodeTextField;
@property (strong, nonatomic) VerifyButton *verifyCodeButton;
@property (strong, nonatomic) UIButton *nextButton;

@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"忘记密码";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if(self.verifyCodeButton) {
        [self.verifyCodeButton stop];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Private Method
- (void)createUI {
    
    UIImageView *mobilePhoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.0f), TRANS_VALUE(9.0f), TRANS_VALUE(16.0f), TRANS_VALUE(22.0f))];
    mobilePhoneImageView.backgroundColor = I_COLOR_WHITE;
    mobilePhoneImageView.contentMode = UIViewContentModeScaleAspectFit;
    mobilePhoneImageView.image = [UIImage imageNamed:@"ic_login_mobile_phone_1"];
    [self.view addSubview:mobilePhoneImageView];
    
    UILabel *mobilePhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(0.0f), TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    mobilePhoneLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    mobilePhoneLabel.textColor = I_COLOR_BLACK;
    mobilePhoneLabel.text = @"手机号";
    [self.view addSubview:mobilePhoneLabel];
    
    self.mobilePhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(0.0f), TRANS_VALUE(220.0f), TRANS_VALUE(40.0f))];
    self.mobilePhoneTextField.borderStyle = UITextBorderStyleNone;
    self.mobilePhoneTextField.backgroundColor = I_COLOR_WHITE;
    self.mobilePhoneTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.mobilePhoneTextField.textColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.mobilePhoneTextField.tintColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.mobilePhoneTextField];
    self.mobilePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f), SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView1];
    
    UIImageView *verifyCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.5f), TRANS_VALUE(10.5f) + TRANS_VALUE(40.0f) + 1.0f , TRANS_VALUE(19.0f), TRANS_VALUE(19.0f))];
    verifyCodeImageView.backgroundColor = I_COLOR_WHITE;
    verifyCodeImageView.contentMode = UIViewContentModeScaleAspectFit;
    verifyCodeImageView.image = [UIImage imageNamed:@"ic_login_verify_code_1"];
    [self.view addSubview:verifyCodeImageView];
    
    UILabel *verifyCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    verifyCodeLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    verifyCodeLabel.textColor = I_COLOR_BLACK;
    verifyCodeLabel.text = @"验证码";
    [self.view addSubview:verifyCodeLabel];
    
    self.verifyCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(130.0f), TRANS_VALUE(40.0f))];
    self.verifyCodeTextField.borderStyle = UITextBorderStyleNone;
    self.verifyCodeTextField.backgroundColor = I_COLOR_WHITE;
    self.verifyCodeTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.verifyCodeTextField.textColor = I_COLOR_BLACK;
    self.verifyCodeTextField.textColor = I_COLOR_BLACK;
    self.verifyCodeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.verifyCodeTextField.tintColor = I_COLOR_BLACK;
    self.verifyCodeTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.verifyCodeTextField];
    self.verifyCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(230.0f), TRANS_VALUE(40.0f) + 1.0f + TRANS_VALUE(5.0f)  ,TRANS_VALUE(1.0f), TRANS_VALUE(30.0f))];
    dividerView2.backgroundColor = I_COLOR_GRAY;
    [self.view addSubview:dividerView2];
    
    self.verifyCodeButton = [[VerifyButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - TRANS_VALUE(88.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(87.0f), TRANS_VALUE(40.0f)) withTitle:@"获取验证码"];
    self.verifyCodeButton.backgroundColor = I_COLOR_WHITE;
    [self.verifyCodeButton setTitleColor:I_COLOR_BLACK];
    [self.view addSubview:self.verifyCodeButton];
    [self.verifyCodeButton addTarget:self action:@selector(verifyCodeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *dividerView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(80.0f) + 2.0f, SCREEN_WIDTH, 1.0f)];
    dividerView3.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView3];
    
    self.nextButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(25.0f), TRANS_VALUE(96.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(25.0f), TRANS_VALUE(36.0f))];
    self.nextButton.clipsToBounds = YES;
    self.nextButton.layer.borderColor = I_COLOR_GRAY.CGColor;
    self.nextButton.layer.borderWidth = 1.0f;
    self.nextButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.nextButton setTitleColor:I_COLOR_BLACK forState:UIControlStateNormal];
    [self.nextButton setTitleColor:I_COLOR_BLACK forState:UIControlStateSelected];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateSelected];
    [self.view addSubview:self.nextButton];
    
    [self.nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)loadData {
    
}

#pragma mark - UIButton Action
//验证码按钮
- (void)verifyCodeButtonAction:(id)sender {
    NSString *mobile = self.mobilePhoneTextField.text;
    if([NSString isBlankString:mobile]) {
        [MBProgressHUD showToast:@"请输入手机号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:mobile]) {
        [MBProgressHUD showToast:@"请输入正确的手机号码" initWithView:self.view];
        return;
    }
    VerifyButton *button = (VerifyButton *)sender;
    [button start];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:mobile forKey:@"mobile"];
    [SVProgressHUD showWithStatus:@"正在获取验证码..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getVerifyCodeWithParams:params success:^(NSString *verifyCode) {
        [SVProgressHUD dismiss];
        if(verifyCode != nil) {
            [MBProgressHUD showToast:@"已发送验证码, 请注意查收" initWithView:self.view];
        } else {
            [MBProgressHUD showToast:@"获取验证码失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

//下一步按钮点击事件
- (void)nextButtonAction:(id)sender {
    NSString *mobilePhone = self.mobilePhoneTextField.text;
    NSString *verifyCode = self.verifyCodeTextField.text;
    ResetPasswordViewController *resetPasswordViewController = [[ResetPasswordViewController alloc] init];
    resetPasswordViewController.mobilePhone = mobilePhone;
    resetPasswordViewController.verifyCode = verifyCode;
    [self.navigationController pushViewController:resetPasswordViewController animated:YES];
}

@end
